package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.R;
import com.itheima.dao.SysDeptDao;
import com.itheima.mapper.SysUserMapper;
import com.itheima.pojo.SysDept;
import com.itheima.pojo.SysUser;
import com.itheima.pojo.TreeSelect;
import com.itheima.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysDeptServiceImpl implements SysDeptService {

    @Autowired
    private SysDeptDao sysDeptDao;

    @Autowired
    private SysUserMapper userMapper;

    @Override
    public R treeselect() {
        LambdaQueryWrapper<SysDept> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDept::getParentId,0);
        List<SysDept> sysDepts = sysDeptDao.selectList(wrapper);

        List<TreeSelect> list=new ArrayList<>();
        for (SysDept sysDept : sysDepts) {
            TreeSelect treeSelect = new TreeSelect();
            treeSelect.setId(sysDept.getDeptId());
            treeSelect.setLabel(sysDept.getDeptName());
            list.add(treeSelect);
        }

        for (TreeSelect treeSelect : list) {
            LambdaQueryWrapper<SysDept> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(SysDept::getParentId,treeSelect.getId());
            List<TreeSelect> children=new ArrayList();
            List<SysDept> sysDepts1 = sysDeptDao.selectList(wrapper1);
            for (SysDept sysDept : sysDepts1) {
                TreeSelect treeSelect1 = new TreeSelect();
                treeSelect1.setId(sysDept.getDeptId());
                treeSelect1.setLabel(sysDept.getDeptName());
                children.add(treeSelect1);
            }
            treeSelect.setChildren(children);
        }

        for (int i = 0; i < list.size(); i++) {
            TreeSelect treeSelect = list.get(i);//传智教育
            List children = treeSelect.getChildren();
            for (int j = 0; j < children.size(); j++) {
                TreeSelect treeSelect1 = (TreeSelect) children.get(j);//北京总公司
                LambdaQueryWrapper<SysDept> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(SysDept::getParentId,treeSelect1.getId());
                List<SysDept> sysDepts1 = sysDeptDao.selectList(wrapper1);

                List<TreeSelect> child = new ArrayList<>();
                for (SysDept sysDept : sysDepts1) {//研发部门
                    TreeSelect treeSelect2 = new TreeSelect();
                    treeSelect2.setId(sysDept.getDeptId());
                    treeSelect2.setLabel(sysDept.getDeptName());

                    LambdaQueryWrapper<SysDept> wrapper2 = new LambdaQueryWrapper<>();
                    wrapper2.eq(SysDept::getParentId,treeSelect2.getId());
                    List<SysDept> sysDepts2 = sysDeptDao.selectList(wrapper2);
                    List<TreeSelect> child1=new ArrayList<>();
                    for (SysDept dept : sysDepts2) {//研发一部门
                        TreeSelect treeSelect3 = new TreeSelect();
                        treeSelect3.setId(dept.getDeptId());
                        treeSelect3.setLabel(dept.getDeptName());
                        child1.add(treeSelect3);
                    }
                    treeSelect2.setChildren(child1);

                    child.add(treeSelect2);
                }
                treeSelect1.setChildren(child);
            }
        }
        return new R(1,"success",list);
    }

    @Override
    public R findUserList(Long deptId) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getDeptId,deptId);
        List<SysUser> sysUsers = userMapper.selectList(wrapper);
        return new R(1,"操作成功",sysUsers);
    }

    @Override
    public R list() {
        List<SysDept> sysDepts = sysDeptDao.selectList(null);
        return new R(1,"返回部门列表!",sysDepts);
    }
}
